package com.sendi.mobilework.pager.ext;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sendi.mobilework.NewsDetailActivity;
import com.sendi.mobilework.R;
import com.sendi.mobilework.api.Api;
import com.sendi.mobilework.datamodel.CompanyNews;
import com.sendi.mobilework.global.GlobalContent;
import com.sendi.mobilework.pager.BasePager;
import com.sendi.mobilework.util.CacheUtils;
import com.sendi.mobilework.util.HttpUtils;
import com.sendi.mobilework.view.PointView;
import com.sendi.mobilework.view.RefreshListView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 公司动态页面
 * Created by Administrator on 2017/3/22.
 */

public class CompanyPager extends BasePager {
    private String TAG="SENDI";

    private RefreshListView mListView;
    private LinearLayout mLRoot;
    private List<CompanyNews.RecordBean> mCompanyNewsList;//存放公司动态的集合
    private final String ACTION="findNews";
    private NewsAdapter mAdapter;

    private Api mApi;
    public CompanyPager(Activity activity) {
        super(activity);
        mApi= HttpUtils.getRetrofit(GlobalContent.COMPANY_NEWS_SEVER).create(Api.class);
    }

    @Override
    public void initView() {
        super.initView();
        View view=View.inflate(mActivity, R.layout.company_news_layout,null);
        mListView= (RefreshListView) view.findViewById(R.id.company_listview);
        mTitleText.setText("动态");
        mFrameLayout.addView(view);
        mListView.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新数据
                getDataFromSever();
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i(TAG, "onItemClick: "+position);
                Intent intent=new Intent(mActivity, NewsDetailActivity.class);
                intent.putExtra("id",mCompanyNewsList.get(position).getId());
                mActivity.startActivity(intent);
            }
        });

    }

    @Override
    public void initData() {
        mCompanyNewsList=new ArrayList<>();
        mAdapter=new NewsAdapter();
        String cache=CacheUtils.getCache(mActivity,GlobalContent.COMPANY_NEWS_SEVER+ACTION);
        if (!TextUtils.isEmpty(cache)){
            Gson gson = new Gson();
            mCompanyNewsList=gson.fromJson(cache,CompanyNews.class).getRecord();
            Log.i(TAG, "initData: "+cache);
            if (mCompanyNewsList!=null){
                mListView.setAdapter(mAdapter);
            }
        }
        getDataFromSever();

    }

    /**
     * 从网络获取数据
     */
    private void getDataFromSever() {
        mApi.getCompanyNews().enqueue(new Callback<CompanyNews>() {
            @Override
            public void onResponse(Response<CompanyNews> response) {
                Log.i(TAG, "onResponse: "+response.body().toString());
                mCompanyNewsList=response.body().getRecord();
                Gson mGson=new Gson();
                if (response.isSuccess()){
                    mListView.setAdapter(mAdapter);
                    CacheUtils.setCache(mActivity, GlobalContent.COMPANY_NEWS_SEVER+ACTION,
                            mGson.toJson(response.body()));
                    mListView.refreshComplete(true);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.i(TAG, "onFailure: "+t.getMessage());
                mListView.refreshComplete(false);
            }
        });
    }

    class NewsAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return mCompanyNewsList.size();
        }

        @Override
        public Object getItem(int position) {
            return mCompanyNewsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=null;
            ViewHolder holder=null;
            if (convertView==null){
                view=View.inflate(mActivity,R.layout.company_news_item,null);
                holder=new ViewHolder();
                holder.content= (TextView) view.findViewById(R.id.tv_content);
                view.setTag(holder);
            }else {
                view=convertView;
                holder= (ViewHolder) view.getTag();
            }

            holder.content.setText(mCompanyNewsList.get(position).getTitle());

            return view;
        }
    }

    class ViewHolder{
        public TextView content;
    }
}
